<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of cart
 *
 * @author Hashan
 */


class cart {

//    function  __construct() {
//        $this->dbLink = new Connection();
//    }

    function writeShoppingCart() {
        $cart = $_SESSION['cart'];
        if (!$cart) {
            return '<p>You have no items in your shopping cart</p>';
        } else {
// Parse the cart session variable
            $items = explode(',',$cart);
            $s = (count($items) > 1) ? 's':'';
            return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
        }
    }

    function sortCart() {
        $cart = $_SESSION['cart'];
        if ($cart) {
            $items = explode(',',$cart);
            $contents = array();
            foreach ($items as $item) {
                $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
            }
        }
    }


    function prepareCart() {
        $total = 0;
        $output[] = '<table>';
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM mp_tb_product WHERE MP_PRD_ID = '.$id;
            $this->dbLink = new Connection();
            $result = $this->dbLink->query($sql);
            $row = $result->fetch();
            extract($row);
            $output[] = '<tr>';
            $output[] = '<td><a href="CartPage.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
            $output[] = '<td>'.$row['MP_PRD_BRAND'].' '.$row['MP_PRD_MODEL'].'</td>';
            $output[] = '<td>&pound;'.$row['MP_PRD_PRICE'].'</td>';
            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
            $output[] = '<td>&pound;'.($row['MP_PRD_PRICE']* $qty).'</td>';
            $total += $row['MP_PRD_PRICE'] * $qty;
            $output[] = '</tr>';
        }
        $output[] = '</table>';
        $output[] = '<p>Grand total: &pound;'.$total.'</p>';
    }

//    function updateCart() {
//        $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
//        $output[] = '<table>';
//        foreach ($contents as $id=>$qty) {
//            $sql = 'SELECT * FROM mp_tb_product WHERE MP_PRD_ID = '.$id;
//            $this->dbLink = new Connection();
//            $result = $this->dbLink->query($sql);
//            $row = $result->fetch();
//            extract($row);
//            $output[] = '<tr>';
//            $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
//            $output[] = '<td>'.$row['MP_PRD_BRAND'].' '.$row['MP_PRD_MODEL'].'</td>';
//            $output[] = '<td>&pound;'.$row['MP_PRD_PRICE'].'</td>';
//            $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
//            $output[] = '<td>&pound;'.($row['MP_PRD_PRICE'] * $qty).'</td>';
//            $total += $row['MP_PRD_PRICE'] * $qty;
//            $output[] = '</tr>';
//        }
//        $output[] = '</table>';
//        $output[] = '<p>Grand total: &pound;'.$total.'</p>';
//        $output[] = '<div><button type="submit">Update cart</button></div>';
//        $output[] = '</form>';
//    }

    function showCart() {
        $cart = $_SESSION['cart'];
        if ($cart) {
            $items = explode(',',$cart);
            $contents = array();
            foreach ($items as $item) {
                $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
            }
            $output[] = '<form action="CartPage.php?action=update" method="post" id="cart">';
            $output[] = '<table>';
            foreach ($contents as $id=>$qty) {
                $sql = 'SELECT * FROM mp_tb_product WHERE MP_PRD_ID = '.$id;
                $this->dbLink = new Connection();
                $result = $this->dbLink->query($sql);
                $row = $row=mysql_fetch_array($result,MYSQL_ASSOC);
                extract($row);
                $output[] = '<tr>';
                $output[] = '<td><a href="CartPage.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
                $output[] = '<td>'.$row['MP_PRD_BRAND'].' '.$row['MP_PRD_MODEL'].'</td>';
                $output[] = '<td>&pound;'.$row['MP_PRD_PRICE'].'</td>';
                $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
                $output[] = '<td>Rs. ;'.($row['MP_PRD_PRICE'] * $qty).'</td>';
                $total += $row['MP_PRD_PRICE'] * $qty;
                $output[] = '</tr>';
            }
            $output[] = '</table>';
            $output[] = '<p>Grand total: &pound;'.$total.'</p>';
            $output[] = '<div><button type="submit">Update cart</button></div>';
            $output[] = '</form>';
        } else {
            $output[] = '<p>You shopping cart is empty.</p>';
        }
        return join('',$output);
    }

}

?>
